<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../vue.js"></script>
</head>

<body>
  <!-- 需求: 计数器 -->
  <!-- 操作在子组件中完成 -->
  <!-- 展示交给父组件完成 -->

  <!-- 父组件模版 -->
  <div id="app">
    <p>{{num}}</p>
    <btn @num-de="decreNum" @num-in="increNum"></btn>
  </div>

  <!-- 子组件模板 -->
  <template id="btn">
    <div>
      <button @click="decrement">-</button>
      <button @click="increment">+</button>
    </div>
  </template>
  <script>
    // 子组件模块
    const btn = {
      template: "#btn",
      methods: {
        decrement() {
          this.$emit("num-de")
        },
        increment() {
          this.$emit("num-in")
        }
      }
    }

    // 父组件模块
    const app = new Vue({
      el: "#app",
      data: {
        num: 0
      },
      components: {
        btn
      },
      methods: {
        increNum() {
          this.num++
        },
        decreNum() {
          this.num--
        }
      }
    })
  </script>
</body>

</html>